<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="抓拍时间" prop="captureTime">
        <el-date-picker clearable
          v-model="queryParams.captureTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择抓拍时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备" prop="labelId">
        <el-select v-model="queryParams.securityFacilitesId" :clearable="true" placeholder="请选择设备">
          <el-option
            v-for="item in task"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deploy:alarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--告警记录-->
    <template>
      <div id="box">
        <ul class="alarm-list">
          <li v-for="(v, index) in alarmList" :key="index" class="product-item">
            <div class="product-content2">
              <div>
                <image-preview :src="v.image" alt="Dynamic Image" class="fixed-image-size3"/>
                <div>
                  <div>抓拍时间：{{ v.captureTime }}</div>
<!--                  <div>-->
<!--                    告警类型：-->
<!--                    <dict-tag :options="dict.type.alarm_type" :value="v.type"></dict-tag>-->
<!--                  </div>-->
                  <div>
                    相似度：
                    <el-progress
                      :percentage="v.similarity"
                      :color="[
                      {color: '#f56c6c', percentage: 50},
                      {color: '#e6a23c', percentage: 70},
                      {color: '#6f7ad3', percentage: 100}
                    ]"
                    ></el-progress>
                  </div>
                </div>
              </div>
              <div class="info">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-info"
                  @click="showInfo(v)"
                >详情</el-button>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </template>

<!--    <el-table v-loading="loading" :data="alarmList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="id" align="center" prop="id" />-->
<!--      <el-table-column label="抓拍时间" align="center" prop="captureTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ scope.row.captureTime }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="告警类型" align="center" prop="type">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.alarm_type" :value="scope.row.type"></dict-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="抓拍照片" align="center" prop="image" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="相似度" align="center" prop="similarity">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.similarity * 100 }} %-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--&lt;!&ndash;      <el-table-column label="任务subject" align="center" prop="subject" />&ndash;&gt;-->
<!--&lt;!&ndash;      <el-table-column label="状态" align="center" prop="status" />&ndash;&gt;-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-info"-->
<!--            @click="showInfo(scope.row)"-->
<!--          >详情</el-button>-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['deploy:alarm:edit']"&ndash;&gt;-->
<!--&lt;!&ndash;          >修改</el-button>&ndash;&gt;-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['deploy:alarm:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

    <template>
      <div v-if="total === 0" class="center-content">
        <hr />
        <h3>暂无告警记录</h3>
        <hr />
      </div>
    </template>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改抓拍记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="抓拍时间" prop="captureTime">
          <el-date-picker clearable
            v-model="form.captureTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择抓拍时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="抓拍照片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="设备id" prop="securityFacilitesId">
          <el-input v-model="form.securityFacilitesId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="相似度" prop="similarity">
          <el-input v-model="form.similarity" placeholder="请输入相似度" />
        </el-form-item>
        <el-form-item label="任务subject" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入任务subject" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  告警详情弹框  -->
    <el-dialog title="告警详情" :visible.sync="infoDialog" width="500px" append-to-body :close-on-click-modal="false">
      <el-row>
        <el-col :span="12">
          <el-form ref="form" :model="info" label-width="80px">
            <h3>
              抓拍时间：{{ info.captureTime }}
            </h3>
            <h3>
              抓拍照片：<el-image :src="info.image" style="width: 50px; height: 50px"/>
            </h3>
            <h3>
              设备id：{{ info.securityFacilitesId }}
            </h3>
            <h3>
              相似度：{{ info.similarity  }} %
            </h3>
            <h3>
              任务subject：{{ info.subject }}
            </h3>
          </el-form>
        </el-col>
        <el-col :span="12">
          <div>
            <h3>
              布控图片：
            </h3>
            <el-carousel :interval="4000" trigger="click">
              <el-carousel-item v-for="(item, index) in taskImages" :key="index">
                <img :src="item" alt="布控图片" style="width: 100%; height: auto;" />
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="infoDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlarm, getAlarm, delAlarm, addAlarm, updateAlarm } from "@/api/deploy/alarm";
import {getTaskFace} from "../../../api/deploy/face";
import { getSecurityFacilitiesCount } from '@/api/community/facilities'

export default {
  name: "Alarm",
  dicts: ['alarm_type'],
  data() {
    return {
      // 设备表单
      task: [],
      // 详情弹窗
      infoDialog: false,
      // 告警详情
      info: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抓拍记录表格数据
      alarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        captureTime: null,
        type: null,
        image: null,
        securityFacilitesId: null,
        similarity: null,
        subject: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskImages: []
    };
  },
  created() {
    this.queryParams.taskId = this.$route.query.taskId;
    this.getList();
    this.getSecurityFacilitiesCount();
  },
  methods: {
    // 实有设备下拉框查询
    getSecurityFacilitiesCount() {
      getSecurityFacilitiesCount().then(response => {
        this.task = response.data;
      });
    },
    showInfo(info) {
      this.info = info;
      getTaskFace(info.subject.split(',')[1]).then(response => {
        if (response.code === 200){
          this.taskImages = response.data;
        }
      })
      this.infoDialog = true;
    },
    /** 查询抓拍记录列表 */
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        captureTime: null,
        type: null,
        image: null,
        securityFacilitesId: null,
        similarity: null,
        subject: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抓拍记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlarm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改抓拍记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除抓拍记录编号为"' + ids + '"的数据项？').then(function() {
        return delAlarm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deploy/alarm/export', {
        ...this.queryParams
      }, `alarm_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.product-content2 .fixed-image-size3 {
  display: block; /* 确保图片是块级元素 */
  margin: 0 auto 10px auto; /* 图片居中并留下一定的底部间距 */
  max-width: 100%; /* 图片最大宽度为容器宽度 */
  height: 200px; /* 高度自适应，保持比例 */
  width: 200px; /* 固定宽度，可以根据需要调整 */
}
.product-item {
  width: calc(20% - 20px); /* 计算每个项目的宽度，减去外边距 */
  margin: 10px; /* 外边距 */
  list-style: none;
}
#box {
  width: 100%;
  display: flex;
  justify-content: center; /* 居中显示 */
}
.alarm-list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding: 0;
  margin: 0;
  list-style: none;
}
.center-content {
  text-align: center;
}

.center-content hr {
  margin: auto;/* 或者根据需要调整宽度 */
}
</style>
